home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt86aug.arc / MATHREC.ARC / PALIND.BAS < prev    next >
BASIC Source File  |  1980-01-01  |  2KB  |  69 lines

  1. 10 REM --------< Palindromic Sums Routine >--------
  2. 20 REM --------<       Bob Kurosaka       >--------
  3. 30 REM 
  4. 40 SP$=" "          :REM One space inside quotes
  5. 50 D$="0123456789"
  6. 60 YES=(1=1)
  7. 70 DIM A(100)       :REM A() holds the the digits.
  8. 80 CLS
  9. 90 PRINT "Program generates sequences that end 
  10.    in palindromes."
  11. 100 PRINT
  12. 110 PRINT "Lower limit (>10) and upper limit ";
  13. 120 INPUT LL, UL
  14. 130 LL=ABS(INT(LL))
  15. 140 UL=ABS(INT(UL))
  16. 150 IF LL<10 THEN 110
  17. 160 FOR N=LL TO UL
  18. 170 SP=0    :REM SP counts the steps before a cycle
  19. 180 REM
  20. 190 REM Break up the term into its component digits
  21. 200 M=N         :REM Make a copy of latest term
  22. 210 D=1         :REM D = no. of digits
  23. 220 T=INT(M/10) :REM T = no. of "Tens" in M
  24. 230 A(D)=M-10*T :REM Store rightmost digit in 
  25.                  array A
  26. 240 IF T<>0 THEN D=D+1: M=T: GOTO 220
  27. 250 ODD=ABS((INT(D/2)<>D/2))    :REM Even or odd 
  28.                                  no. of digits?
  29. 260 REM
  30. 270 REM Print the latest term
  31. 280 FOR I=D TO 1 STEP -1
  32. 290 PRINT MID$(D$,A(I)+1,1);
  33. 300 NEXT I
  34. 310 PRINT SP$;
  35. 320 REM
  36. 330 REM Check for palindrome
  37. 340 FOR I=1 TO D/2
  38. 350 PL=(A(I)=A(D-I+1))
  39. 360 IF NOT PL THEN I=D/2 :REM Exit from loop if 
  40.     no. is not a pal.
  41. 370 NEXT I
  42. 380 IF PL THEN 580
  43. 390 REM
  44. 400 REM Add each digit to its reverse image 
  45.     counterpart
  46. 410 FOR I=1 TO D/2+ODD
  47. 420 A(I)=A(I)+A(D-I+1)
  48. 430 A(D-I+1)=A(I)
  49. 440 NEXT I
  50. 450 REM Check for carry
  51. 460 FOR I=1 TO D
  52. 470 IF A(I)<10 THEN 500
  53. 480 A(I)=A(I)-10
  54. 490 A(I+1)=A(I+1)+1
  55. 500 NEXT I
  56. 510 IF A(D+1)=0 THEN 540
  57. 520 D=D+1
  58. 530 ODD=ABS((INT(D/2)<>D/2))
  59. 540 SP=SP+1
  60. 550 GOTO 280
  61. 560 REM
  62. 570 REM Indicate that a cycle has been found
  63. 580 PRINT "* at step "; SP
  64. 590 FOR I=1 TO D
  65. 600 A(I)=0
  66. 610 NEXT I
  67. 620 NEXT N
  68. 630 END
  69.